﻿function goBack() {
    if (isBack) {
        location.href = '/Questions/QuestionList';
    }
    else {
        location.href = '/Questions/QuestionAdd';
    }
}
var isBack = true;
function chengeBack(op) {
    isBack = op;
}
function SubData() {
    var alist = new Array();
    var centext = $("textarea[name='AnswerCentext']");
    centext.each(function () {
        alist.push({
            AnswerContent: $(this).html(),
            IsTrueAnswer: false
        });
    });

    $("input[type='checkbox']:checked").each(function () {
        alist[$(this).val() - 1].IsTrueAnswer = true;
    });
    var postData = {
        QuestionType: $("#TestType").val(),
        LibraryID: "b5ac5949-f6ed-4d6b-a859-6baa424c77cd",
        ModalityID: $("#ModalitySelect").val(),
        ProductID: $("#ProductSelect").val(),
        QLevel: $("#Levelselect").val(),
        Block: $("#BlockSelect").val(),
        IsActive: $("#PIsActive").val(),
        AnswerList: alist
    };
    alert(postData.AnswerList[0].IsTrueAnswer);
    $.post('/Questions/Addinfo',postData, function (result) {
        if (result == "suc") {
            goBack();
        }
        else if (result == "repeat") {
            $.alert({
                title: '系统提示',
                content: '重复数据无法添加！',
                confirm: function () {
                }
            });
        }
        else if (result == "nouser") {
            location.href = '/Login/Index';
        }
        else {
            $.alert({
                title: '系统提示',
                content: '操作失败！',
                confirm: function () {
                }
            });
        }
    });
}
var index = 2;
$(document).ready(function () {

    $('#addQuestionForm')
          .bootstrapValidator({
              message: 'This value is not valid',
              feedbackIcons: {
                  valid: 'glyphicon glyphicon-ok',
                  invalid: 'glyphicon glyphicon-remove',
                  validating: 'glyphicon glyphicon-refresh'
              },
              fields: {
                  TestType: {
                      message: 'The Test Type is not valid',
                      validators: {
                          notEmpty: {
                              message: 'The Test Type is required and can\'t be empty'
                          }
                      }
                  },
                  TestType: {
                      message: 'The Test Type is not valid',
                      validators: {
                          notEmpty: {
                              message: 'The Test Type is required and can\'t be empty'
                          }
                      }
                  },
                  ModalitySelect: {
                      message: 'The Modality is not valid',
                      validators: {
                          notEmpty: {
                              message: 'The Modality is required and can\'t be empty'
                          }
                      }
                  },
                  ProductSelect: {
                      message: 'The Product is not valid',
                      validators: {
                          notEmpty: {
                              message: 'The Product is required and can\'t be empty'
                          }
                      }
                  },
                  Levelselect: {
                      message: 'The Level is not valid',
                      validators: {
                          notEmpty: {
                              message: 'The Level is required and can\'t be empty'
                          }
                      }
                  },
                  BlockSelect: {
                      message: 'The Block is not valid',
                      validators: {
                          notEmpty: {
                              message: 'The Block is required and can\'t be empty'
                          }
                      }
                  },
                  PIsActive: {
                      message: 'The Status is not valid',
                      validators: {
                          notEmpty: {
                              message: 'The Status is required and can\'t be empty'
                          }
                      }
                  },
                  QuestionCentext: {
                      message: 'The Test Question is not valid',
                      validators: {
                          notEmpty: {
                              message: 'The Test Question is required and can\'t be empty'
                          }
                      }
                  },
                  'AnswerCentext': {
                      message: 'The value is not valid',
                      validators: {
                          notEmpty: {
                              message: 'The value is required and can\'t be empty'
                          }
                      }
                  },
                  Standard: {
                      message: 'The Standard Test Answer is not valid',
                      validators: {
                          notEmpty: {
                              message: 'The Standard Test Answer is required and can\'t be empty'
                          }
                      }
                  }
              }
          })
          .on('success.form.bv', function (e) {
              e.preventDefault();
              SubData();
          });

    $("#Addspan").click(function () {
        var st = String.fromCharCode(65 + index)
        var strhtml = " <div class='form-group'><div class='col-sm-12'><div class='col-sm-2'><label class='control-label'><span>*</span>";
        strhtml += "Answer&nbsp;Option";
        strhtml += st;

        strhtml += "：</label>";
        strhtml += " </div><div class='col-sm-10'>";
        strhtml += "<textarea name='AnswerCentext' class='form-control' id='Answer" + st + "Centext' rows='5' maxlength='200'></textarea>";
        strhtml += " </div> </div> </div>";

        $("#Addpan").before(strhtml);
        index++;
        $(".standardul").append("<li><input type='checkbox' id=" + st + " name='Standard' value=" + index + " /><label for=" + st + ">" + st + "</label></li>");
  
    });
});
